package com.bingo.demo.utils;

import com.alibaba.fastjson.JSONObject;
import com.qiniu.common.QiniuException;
import com.qiniu.common.Zone;

import com.qiniu.http.Response;
import com.qiniu.storage.BucketManager;
import com.qiniu.storage.Configuration;
import com.qiniu.storage.UploadManager;
import com.qiniu.util.Auth;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

public class uploadUtil {

    public static String uploadPicture(MultipartFile file) throws IOException {
        String realPath = System.getProperty("user.dir");
        System.out.println(realPath);
        String path = realPath+"\\src\\main\\resources\\static\\img";
        // 1.获取上传文件的名字 String
        String[] split = file.getOriginalFilename().split("\\\\");
        String oriName=split[split.length-1];
        String fileName = UUID.randomUUID().toString()+oriName;
        // 2.创建文件实例
        File filePath=new File(path,fileName);
        System.out.println("filePath"+filePath);
        // 3.写入文件
        if (!filePath.getParentFile().exists()) {
            filePath.getParentFile().mkdirs();
        }
        file.transferTo(filePath);
        return "img/" +fileName;
    }

    public static boolean deletePicture(String filePath){
        String projectPath = System.getProperty("user.dir");
        String[] split = filePath.split("/");
        String path = split[split.length-1];
        String realPath = projectPath+"\\src\\main\\resources\\static\\img\\"+path;
        File file=new File(realPath);
        //如果是一个文件且不为空则删除
        if(file.isFile()&&file.exists()){
            file.delete();
            return true;
        }
        return false;
    }

    public static String qiniuUpload(MultipartFile file){
        Configuration cfg = new Configuration(Zone.huanan());
        UploadManager uploadManager = new UploadManager(cfg);
        Auth auth = Auth.create(qiniuKey.AccessKey,qiniuKey.SecretKey);
        String token = auth.uploadToken(qiniuKey.buckey);
        // 1.获取上传文件的名字 String
        String[] split = file.getOriginalFilename().split("\\.");
        System.out.println(split.length);
        String oriName=split[split.length-1];
        System.out.println(oriName);
        String fileName = UUID.randomUUID().toString()+"."+oriName;
        // 2.上传图片
        try {
            Response response = uploadManager.put(file.getBytes(), fileName, token);
            System.out.println(response.bodyString());
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "http://"+qiniuKey.domain+"/"+fileName;
    }

    public static boolean qiniuDelete(String fileName){
        Auth auth = Auth.create(qiniuKey.AccessKey,qiniuKey.SecretKey);
        Configuration cfg = new Configuration(Zone.huanan());
        BucketManager bucketManager = new BucketManager(auth,cfg);
        String[] split = fileName.split("/");
        String key = split[split.length-1];
         try {
            bucketManager.delete(qiniuKey.buckey,key);
            return true;
        } catch (QiniuException e) {
            e.printStackTrace();
        }
        return false;
    }

}
